package com.zyh.controller;


import com.zyh.entity.*;
import com.zyh.service.BookService;

import com.zyh.service.EmailService;
import com.zyh.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private EmailService emailService;
    @Autowired
    private StatisticService statisticService;


    //查询所有的用户信息
    @GetMapping("/getAllBook")
    @ResponseBody
    public Map<String, Object> getAllBook(Book book) {
        if (book.getPage() == 0) {
            book.setPage(1);
        }
        Integer start = (book.getPage() - 1) * book.getLimit();
        book.setStart(start);
        Map<String, Object> map = new HashMap<>();
        List<Book> allBook = bookService.getAllBook(book);
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", bookService.getBookCount(book));
        map.put("data", allBook);
        return map;
    }


    @GetMapping("/getBorrowBook")
    @ResponseBody
    public Map<String, Object> getBorrowBook(BorrowBookVo borrowBookVo, Book book) {
        if (borrowBookVo.getPage() == 0) {
            borrowBookVo.setPage(1);
        }
        Integer start = (borrowBookVo.getPage() - 1) * borrowBookVo.getLimit();
        borrowBookVo.setStart(start);
        Map<String, Object> map = new HashMap<>();
        borrowBookVo.setBook(book);
        List<BorrowBookVo> borrowBook1 = bookService.getBorrowBook(borrowBookVo);
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", bookService.getBorrowCount(borrowBookVo));
        map.put("data", borrowBook1);
        return map;
    }

    @GetMapping("/selectBook{bookid}")
    @ResponseBody
    public Map<String, Object> selectBook(@PathVariable("bookid") Integer id) {
        BorrowBookVo borrowBookVo = bookService.selectBook(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);
        map.put("data", borrowBookVo);
        return map;
    }

    //修改
    @PostMapping("/updateBookWithId")
    @ResponseBody
    public Map<String, Object> updateBookWithId(Book book) {
        Map<String, Object> map = new HashMap<>();
        Integer integer = bookService.updateBookWithId(book);
        map.put("code", 0);
        map.put("msg", "修改成功");
        map.put("count", 1);
        map.put("data", integer);
        return map;
    }

    //添加
    @PostMapping("/addBook")
    @ResponseBody
    public Map<String, Object> addBook(Book book) {
        Map<String, Object> map = new HashMap<>();
        Integer integer = bookService.addBook(book);
        map.put("code", 0);
        map.put("msg", "添加成功");
        map.put("count", 1);
        map.put("data", integer);
        return map;

    }

    //删除
    @GetMapping("/DeleteBookByids")
    @ResponseBody
    public Map<String, Object> DeleteBookByids(Integer[] ids) {
        Map<String, Object> map = new HashMap<>();
        Integer idd = null;
        idd = bookService.DeleteBookByids(ids);
        map.put("code", 0);
        map.put("msg", "删除成功");
        map.put("count", 1);
        map.put("data", idd);
        return map;
    }

    //借书
    @PostMapping("/BorrowBookWithIds")
    @ResponseBody
    public Map<String, Object> BorrowBookWithIds(Integer[] ids, Integer user1) {
        Map<String, Object> map = new HashMap<>();
        Integer idd = null;
        List<BorrowBookVo> map1 = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            BorrowBookVo borrowBookVo = new BorrowBookVo();
            borrowBookVo.getBookidd();
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            borrowBookVo.setLendTime(new Date());
            borrowBookVo.setBookidd(ids[i]);
            borrowBookVo.setUserid(user1);
            map1.add(borrowBookVo);
        }
        idd = bookService.BorrowBookWithIds(ids);
        bookService.addBorrow(map1);
        Statistic statistic = new Statistic();
        statisticService.upadatereturn(statistic);
        map.put("code", 0);
        map.put("msg", "借书成功");
        map.put("count", idd);
        map.put("data", idd);
        return map;
    }


    @PostMapping("/SendBookWithId")
    @ResponseBody
    public Map<String, Object> SendBookWithId(Integer id) {
        Map<String, Object> map = new HashMap<>();
        BorrowBookVo borrowBookVo = new BorrowBookVo();
        borrowBookVo.setSendTime(new Date());
        borrowBookVo.setId(id);
        Integer integer = bookService.SendBookWithId(borrowBookVo);
        Statistic statistic = new Statistic();
        statisticService.upadtebookresidue(statistic);
        map.put("code", 0);
        map.put("msg", "修改成功");
        map.put("count", 1);
        map.put("data", integer);
        return map;
    }


    @GetMapping("/selectbyuserid/{id}")
    @ResponseBody
    public Map<String, Object> selectbyuserid(@PathVariable("id") Integer userid, BorrowBookVo borrowBookVo, Book book) {
        User user = new User();
        user.setId(userid);
        borrowBookVo.setUser(user);
        borrowBookVo.setBook(book);
        List<BorrowBookVo> selectbyuserid = bookService.selectbyuserid(borrowBookVo);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);
        map.put("data", selectbyuserid);
        return map;
    }


    @PostMapping("/sendEmail")
    @ResponseBody
    public ResultEntity sendEmail(Integer id) {
        BorrowBookVo borrowBookVo = new BorrowBookVo();
        borrowBookVo.setId(id);
        BorrowBookVo borrowBookVo1 = emailService.selectEmailByid(borrowBookVo);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String lendTime = format.format(borrowBookVo1.getLendTime());
        String content1 = borrowBookVo1.getUser().getUsername() + "于" + lendTime +
                "借了一本" + borrowBookVo1.getBook().getBookname() + " 请尽快还书";
        emailService.sendEmail(borrowBookVo1.getUser().getEmail(), "催还图书", content1);
        MessagesChildren message = new MessagesChildren();
        message.setContext(content1);
        message.setAvatar("admin/images/avatar.jpg");
        message.setTitle("催还图书");
        message.setForm("管理员");
        message.setMid(1);
        message.setTime(borrowBookVo1.getLendTime());
        message.setUid(borrowBookVo1.getUserid());
        int i = emailService.insertmessage(message);
        ResultEntity entity = new ResultEntity();
        Map<Object, Object> map = new HashMap<>();
        if (i > 0) {
            entity.setCode(1);
            entity.setMsg("发送成功");
        }
        return entity;


    }


}
